package com.qfmy.webapp.service.impl.wallpaper;

import com.qfmy.model.entity.wallpaper.Wallpaper;
import com.qfmy.model.vo.wallpaper.WallpaperVo;
import com.qfmy.webapp.mapper.wallpaper.WallpaperMapper;
import com.qfmy.webapp.service.wallpaper.WallpaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.service.impl.wallpaper
 * @date 2025/11/26
 * @description 壁纸
 */
@Service
@SuppressWarnings("all")
public class WallpaperServiceImpl implements WallpaperService {

    /**
     * 注入mapper
     */
    @Autowired
    private WallpaperMapper wallpaperMapper;

    /**
     * 注入redisTemplate
     */
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 获取每日推荐壁纸
     * @return
     */
    @Override
    public List<WallpaperVo> getJingXuanWallpapers() {
        //创建缓存key
        String key = "wallpaper:jingxuan";
        if (!redisTemplate.hasKey(key)) {
            //从数据库中查询数据
            List<Wallpaper> list = wallpaperMapper.getJingXuanWallpapers();
            //转换对象
            List<WallpaperVo> listVo = convertToVo(list);
            Long ttl = listVo.isEmpty() ? 1 : 12L;
            //将数据保存到redis中
            redisTemplate.opsForValue().set(key, list, ttl, TimeUnit.HOURS);
            return listVo;
        }else{
            return (List<WallpaperVo>) redisTemplate.opsForValue().get(key);
        }
    }

    /**
     * 根据分类ID获取壁纸
     * @param categoryId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<WallpaperVo> getWallpapersByCategoryId(Integer categoryId, Integer pageNum, Integer pageSize) {
        //校验分页参数
        if (pageNum < 1 || pageSize < 1) {
            pageNum = 1;
            pageSize = 12;
        }
        //计算分页
        int offset = (pageNum - 1) * pageSize;
        //从数据库中查询数据
        List<Wallpaper> list = wallpaperMapper.getWallpapersByCategoryId(categoryId, offset, pageSize);
        //转换对象
        return convertToVo(list);
    }

    /**
     * 转换为VO
     * @param list
     * @return
     */
    public List<WallpaperVo> convertToVo(List<Wallpaper> list) {
        //判断集合是否为空
        if (list != null && !list.isEmpty()) {
            return list.stream().map(wallpaper -> {
                WallpaperVo wallpaperVo = new WallpaperVo();
                wallpaperVo.setId(wallpaper.getId());
                wallpaperVo.setTitle(wallpaper.getTitle());
                wallpaperVo.setImageUrl(wallpaper.getImageUrl());
                wallpaperVo.setPublisherName(wallpaper.getPublisherName());
                wallpaperVo.setScore(wallpaper.getScore());
                wallpaperVo.setScoreCount(wallpaper.getScoreCount());
                wallpaperVo.setSummary(wallpaper.getSummary());
                wallpaperVo.setTags(wallpaper.getTags().split(","));
                return wallpaperVo;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
